// src/http.js

import axios from 'axios';
import {ElMessage} from 'element-plus';

// 创建 axios 实例
const http = axios.create({
    baseURL: 'http://localhost:80/api',
    timeout: 0,
});

// 请求拦截器
http.interceptors.request.use(config => {
    // 可以在这里添加 token 等请求头信息
    return config;
}, error => {
    return Promise.reject(error);
});

// 响应拦截器
http.interceptors.response.use(response => {
    if (response.data.code != 0) {
        ElMessage({
            message: response?.data?.message || response.message,
            grouping: true,
            type: 'error'
        });
        return Promise.reject(response);
    } else {
        return response;
    }

}, error => {
    console.error('error', error)
    ElMessage({
        message: error.response?.data?.message || error.message,
        grouping: true,
        type: 'error'
    });
    return Promise.reject(error);
});

export default http;
